﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WCFMINERP.COM;
using WCFMINERP.Contracts.DataContracts;
using WCFMINERP.DisjoinProForOutShop.Presenter;

namespace WCFMINERP.DisjoinProForOutShop.Forms
{
    public partial class frmSetupLattice : Form
    {

        LatticePartitionPresenter pres = new LatticePartitionPresenter();


        public frmSetupLattice()
        {
            InitializeComponent();
        }


        List<LatticePartition> listData = new List<LatticePartition>();

        private void frmSetupLattice_Load(object sender, EventArgs e)
        {
            Bind();            
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void Bind()
        {
            listData = pres.GetLatticePartitionList(InnterManagePresenter.FactoryId);
            gridLatticePartition.DataSource = listData;
            BindComControl();
        }

        /// <summary>
        /// 绑定所有数据
        /// </summary>
        private void BindComControl()
        {
            com_LatticePartition.Properties.Items.Clear();
            foreach (LatticePartition model in listData)
            {
                com_LatticePartition.Properties.Items.Add(model);
            }
            com_LatticePartition.SelectedIndex = 0;
            if (!string.IsNullOrEmpty(Help.LatticePartitionID))
            {
                InnterManagePresenter.latticePartitionModel = listData.FirstOrDefault(p => p.Pid == Convert.ToInt32(Help.LatticePartitionID));
            }

            if (InnterManagePresenter.latticePartitionModel != null && InnterManagePresenter.latticePartitionModel.Pid > 0)
            {
                com_LatticePartition.Text = InnterManagePresenter.latticePartitionModel.LatticeHeadName;
            }
        }

        /// <summary>
        /// 检测值
        /// </summary>
        /// <returns></returns>
        private bool isTextNull()
        {
            if (string.IsNullOrEmpty(txt_LatticeHeadName.Text))
            {
                MessageBox.Show("分区名称不能为空！");
                return false;
            }
            if (string.IsNullOrEmpty(txt_RowsNum.Text))
            {
                MessageBox.Show("行不能为空！");
                return false;
            }
            if (Convert.ToInt32(txt_RowsNum.Text) <= 0)
            {
                MessageBox.Show("行必须大于零！");
                return false;
            }
            if (string.IsNullOrEmpty(txt_ColumnNum.Text))
            {
                MessageBox.Show("列不能为空！");
                return false;
            }
            if (Convert.ToInt32(txt_ColumnNum.Text) <= 0)
            {
                MessageBox.Show("列必须大于零！");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_Add_Click(object sender, EventArgs e)
        {
            if (!isTextNull())
            {
                return;
            }


            if (MessageBox.Show("确定增加这条数据？", "提示", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }

            LatticePartition model = new LatticePartition();
            model.LatticeHeadName = txt_LatticeHeadName.Text;
            model.RowsNum = Convert.ToInt32(txt_RowsNum.Text);
            model.ColumnNum = Convert.ToInt32(txt_ColumnNum.Text);
            model.outShopId = InnterManagePresenter.FactoryId;
            int id = pres.AddLatticePartition(model);
            if (id > 0)
            {
                MessageBox.Show("添加成功");
                Bind();
            }
        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_Refresh_Click(object sender, EventArgs e)
        {
            Bind();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_Update_Click(object sender, EventArgs e)
        {
            LatticePartition model = gridViewLatticePartition.GetFocusedRow() as LatticePartition;
            if (model == null || model.Pid == 0)
            {
                MessageBox.Show("请先选择要修改的数据！");
                return;
            }

            if (!isTextNull())
            {
                return;
            }
            if (MessageBox.Show("确定修改这条数据？", "提示", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            model.LatticeHeadName = txt_LatticeHeadName.Text;
            model.RowsNum = Convert.ToInt32(txt_RowsNum.Text);
            model.ColumnNum = Convert.ToInt32(txt_ColumnNum.Text);
            int id = pres.UpdateLatticePartition(model);
            if (id > 0)
            {

                MessageBox.Show("修改成功！");
                Bind();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_Delete_Click(object sender, EventArgs e)
        {
            LatticePartition model = gridViewLatticePartition.GetFocusedRow() as LatticePartition;
            if (model == null || model.Pid == 0)
            {
                MessageBox.Show("请先选择要删除的数据！");
                return;
            }
            if (MessageBox.Show("确定删除这条数据？", "提示", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            model.State = (int)LatticePartitionState.Alreadydeleted;
            int id = pres.UpdateLatticePartitionState(model);
            if (id > 0)
            {
                MessageBox.Show("删除成功！");
                Bind();
            }
        }

        /// <summary>
        /// 点击行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gridViewLatticePartition_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            LatticePartition dr = gridViewLatticePartition.GetRow(e.RowHandle) as LatticePartition;

            txt_ColumnNum.Text = dr.ColumnNum.ToString();
            txt_LatticeHeadName.Text = dr.LatticeHeadName;
            txt_RowsNum.Text = dr.RowsNum.ToString();

        }

        /// <summary>
        /// 确定选择一个分区
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_comfrm_Click(object sender, EventArgs e)
        {
            LatticePartition model = com_LatticePartition.SelectedItem as LatticePartition;
            if (model == null)
            {
                MessageBox.Show("设置成功！");
                return;
            }
            if (InnterManagePresenter.latticePartitionModel!=null)
            {
                if (InnterManagePresenter.latticePartitionModel.Pid != model.Pid)
                {
                    InnterManagePresenter.latticePartitionModel = model;
                    Help.LatticePartitionID = model.Pid.ToString();
                    MessageBox.Show("设置成功！");
                }
            }
            else
            {
                InnterManagePresenter.latticePartitionModel = model;
                Help.LatticePartitionID = model.Pid.ToString();
                MessageBox.Show("设置成功！");
            }
        }      

    }
}
